<script setup lang="ts">
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { BusItemVO } from '#/api/register/bus/item/model';

import { ref } from 'vue';

// import { useAccess } from '@vben/access';
import { useVbenDrawer, useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { getVxePopupContainer } from '@vben/utils';

import { Popconfirm, Space } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { itemList } from '#/api/register/bus/item';

import busDataModal from './bus-data-modal.vue';
import { columns } from './data';

// const props = defineProps<{ groupId?: number | string }>();

// const { hasAccessByCodes } = useAccess();
const busId = ref<number | string>();
const currGroupId = ref<number | string>();
const taskNameTitle = ref<any>('');
const groupNameTitle = ref<any>('');
const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    // trigger: 'row',
  },
  columns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async () => {
        if (!busId.value) return { rows: [] };

        const rows = await itemList({ busId: busId.value });

        return { rows };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  // 表格全局唯一表示 保存列配置需要用到
  id: 'bus-task-group-bus-index',
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  gridOptions,
});

const [BasicDrawer, drawerApi] = useVbenDrawer({
  class: 'w-[85%]',
  onClosed: handleClosed,
  footer: false,
  onOpenChange: async (isOpen) => {
    if (!isOpen) {
      return null;
    }

    const { id, taskName, groupId, groupName } = drawerApi.getData() as {
      groupId?: number | string;
      groupName?: string;
      id?: number | string;
      taskName?: string;
    };
    busId.value = id;
    currGroupId.value = groupId;
    taskNameTitle.value = taskName;
    groupNameTitle.value = groupName;
  },
});

async function handleClosed() {
  busId.value = undefined;
  currGroupId.value = undefined;
  taskNameTitle.value = '';
  groupNameTitle.value = '';
}

// 新增编辑弹窗
const [BusDataModal, busDataModalApi] = useVbenModal({
  connectedComponent: busDataModal,
});

function handleAdd() {
  busDataModalApi.setData({
    taskGroupId: currGroupId.value,
    busId: busId.value,
  });
  busDataModalApi.open();
}

async function handleEdit(row: BusItemVO) {
  busDataModalApi.setData({
    taskGroupId: currGroupId.value,
    busId: busId.value,
    id: row.id,
  });
  busDataModalApi.open();
}

function handleCopy(row: BusItemVO) {
  busDataModalApi.setData({
    taskGroupId: currGroupId.value,
    busId: busId.value,
    copy: true,
    id: row.id,
  });
  busDataModalApi.open();
}

function handleDelete(row: BusItemVO) {
  console.log(row);
}
</script>

<template>
  <BasicDrawer :title="`${taskNameTitle} - ${groupNameTitle}`">
    <BasicTable>
      <template #table-title>
        <main-title text="巴士列表" />
      </template>
      <template #toolbar-tools>
        <Space>
          <a-button
            type="primary"
            v-access:code="['bus:item:add']"
            @click="handleAdd"
          >
            {{ $t('pages.common.add') }}
          </a-button>
        </Space>
      </template>
      <template #action="{ row }">
        <Space>
          <ghost-button
            v-access:code="['bus:item:edit']"
            @click.stop="handleEdit(row)"
          >
            {{ $t('pages.common.edit') }}
          </ghost-button>
          <ghost-button
            class="btn-success"
            v-access:code="['bus:item:edit']"
            @click.stop="handleCopy(row)"
          >
            复制
          </ghost-button>
          <Popconfirm
            :get-popup-container="getVxePopupContainer"
            placement="left"
            title="确认删除？"
            @confirm="handleDelete(row)"
          >
            <ghost-button
              danger
              v-access:code="['bus:item:remove']"
              @click.stop=""
            >
              {{ $t('pages.common.delete') }}
            </ghost-button>
          </Popconfirm>
        </Space>
      </template>
    </BasicTable>
    <BusDataModal @reload="tableApi.query()" />
  </BasicDrawer>
</template>
